<template>
  <div class="box">
    <div class="header">
      <h2>出港返利毛利报表明细</h2>
      <div style="display: flex; justify-content: flex-end">
        <el-button plain icon="el-icon-upload2" @click="exportData"
          >导出</el-button
        >
      </div>
      <hr />
    </div>
    <div class="content">
      <elltable
        :loading="false"
        :data="tableData.list.data"
        :n="4"
        :th="tableData.fields"
      />
    </div>
    <div class="footer">
      <pagination
        :current-page="form.pageDate.pageNum"
        :page-size="form.pageDate.pageSize"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script>
import {
  outboundgrossprofitdetail,
  exportogpdetail,
} from "@/api/HqFinancePanel";

export default {
  data() {
    return {
      form: {
        pageDate: {
          pageNum: 1,
          pageSize: 10,
        },
      },
      tableData: {
        fields: [],
        list: {
          data: [],
        },
      },
      total: 0,
      loading: null,
    };
  },
  mounted() {
    // if (this.form == null) {
    //   this.$message.error('暂无明细，即将跳转到上一个页面...')
    //   setTimeout(() => {
    //     this.$router.go(-1)
    //     this.loading.close()
    //   }, 2000)
    // } else {
    //   this.getDetail(this.form)
    // }
  },
  beforeDestroy() {
    if (this.loading != null) {
      this.loading.close();
    }
  },
  activated() {
    this.form = this.$store.state.app.cgfmltab;
    // // console.log('Component activated')
    // 进行一些操作比如数据重新请求等
    if (this.form == null) {
      this.$message.error("暂无明细，即将跳转到出港返利毛利报表明细页面...");
      setTimeout(() => {
        this.$router.push({
          path: "OutboundGrossProfit",
        });
        if (this.loading != null) {
          this.loading.close();
        }
      }, 2000);
    } else {
      this.getDetail(this.form);
      // this.loading.close()
    }
  },
  methods: {
    exportData() {
      // this.$notify.info({
      //   title: '开始准备下载...'
      // })
      exportogpdetail(this.form).then((res) => {
        // const url = window.URL.createObjectURL(new Blob([res]), {
        //   type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
        // })
        // const link = document.createElement('a')
        // link.href = url
        // link.setAttribute('download', '出港返利毛利报表明细.xlsx') // 设置下载的文件名
        // document.body.appendChild(link)
        // link.click()
        // link.parentNode.removeChild(link)
        // this.$notify({
        //   title: '下载成功',
        //   type: 'success'
        // })
        this.$notify({
          title: "请前往总部下载中心查看",
          type: "success",
        });
      });
    },
    getDetail(data) {
      this.loading = this.$loading({
        lock: true,
        text: "Loading",
        spinner: "el-icon-loading",
        background: "rgba(255, 255, 255, 0.7)",
      });
      outboundgrossprofitdetail(data).then((res) => {
        if (res.code === 200) {
          this.tableData = res.data;
          this.total = res.data.list.total;
        } else {
          this.tableData = {
            fields: [],
            list: {
              data: [],
            },
          };
          this.total = 0;
        }
        this.loading.close();
      });
    },
    handleSizeChange(size) {
      this.form.pageDate.pageSize = size;
      this.getDetail(this.form);
    },
    handleCurrentChange(current) {
      this.form.pageDate.pageNum = current;
      this.getDetail(this.form);
    },
  },
};
</script>

<style lang="scss" scoped>
.box {
  padding: 20px;
}

.header {
  h2 {
    text-align: center;
    color: skyblue;
  }

  hr {
    color: skyblue;
  }
}

.footer {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
</style>
